// 页面置顶或跳转到指定高度
const backTop = (height = 0) => {
  document.body.scrollTop = document.documentElment.scrollTop = height;
};

// 跳转到页面指定元素
const jumpToElement = (element) => {
  document.querySelector(element).scrollIntoView(true);
};

// 防抖
function debounce(func, wait) {
  let timeout, result;

  return function () {
    const context = this;
    const args = arguments;

    clearTimeout(timeout);
    timeout = setTimeout(function () {
      result = func.apply(context, args);
    }, wait);

    return result;
  };
}

// 节流
function throttle(func, wait) {
  var context, args;
  var previous = 0;

  return function () {
    var now = +new Date();
    context = this;
    args = arguments;
    if (now - previous > wait) {
      func.apply(context, args);
      previous = now;
    }
  };
}
export { backTop, jumpToElement, debounce, throttle };
